package com.pe.pgn.clubpgn.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.pe.pgn.clubpgn.dao.VariableGlobalDao;
import com.pe.pgn.clubpgn.domain.beans.BNVariablesSistema;

@Repository("variableGlobalDao")
public class VariableGlobalDaoImpl implements VariableGlobalDao{

	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	
	public BNVariablesSistema obtenerVariables() {
		
		
		StringBuffer query= new StringBuffer();
		
		query.append("select id, de_parametro, va_valor,st_parametro  from  clpb_parametro order by  id asc ");
		System.out.println(query.toString());	
		
		List<Map<String, Object>>  lista=jdbcTemplate.queryForList(query.toString());
		BNVariablesSistema 	bean= new BNVariablesSistema();
		

		if(!lista.isEmpty()){
			HashMap<String, Object> objMap=new HashMap<String, Object>();
			for(int i=0; i<lista.size();i++){
				
				if(i==0){
					objMap = (HashMap<String, Object>) lista.get(i);
					bean.setUploaddir(objMap.get("id").toString());
					bean.setDeuploaddir(objMap.get("de_parametro").toString());
					bean.setStuploaddir(objMap.get("st_parametro").toString());
					
					
					if(objMap.get("va_valor")==null){
						 bean.setVaUploaddir("");
						}else{
						 bean.setVaUploaddir(objMap.get("va_valor").toString());
					}
										
					if(bean.getStuploaddir().equalsIgnoreCase("Y")){
					 bean.setChkUploaddir(true);
					}else{
						bean.setChkUploaddir(false);
					}
				
				}else if(i==1){
					 objMap = (HashMap<String, Object>) lista.get(i);
					 bean.setHostldap(objMap.get("id").toString());
					 bean.setDehostldap(objMap.get("de_parametro").toString());
					 bean.setSthostldap(objMap.get("st_parametro").toString());
					
					 
					 if(objMap.get("va_valor")==null){
						 bean.setVaHostldap("");
						}else{
						 bean.setVaHostldap(objMap.get("va_valor").toString());
					}					 
					 
					 if(bean.getSthostldap().equalsIgnoreCase("Y")){
						 bean.setChkHostldap(true);
						}else{
						bean.setChkHostldap(false);
					 }
				
				}else if(i==2){
					objMap = (HashMap<String, Object>) lista.get(i);
					bean.setPortldap(objMap.get("id").toString());
					bean.setDeportldap(objMap.get("de_parametro").toString());
					bean.setStportldap(objMap.get("st_parametro").toString());
					
					
					 if(objMap.get("va_valor")==null){
						 bean.setVaPortldap("");
						}else{
						 bean.setVaPortldap(objMap.get("va_valor").toString());
					}
					
					if(bean.getStportldap().equalsIgnoreCase("Y")){
						 bean.setChkPortldap(true);
						}else{
						 bean.setChkPortldap(false);
						}
					
				}else if(i==3){
					objMap =(HashMap<String, Object>)  lista.get(i);
					bean.setBasedn(objMap.get("id").toString());
					bean.setDebasedn(objMap.get("de_parametro").toString());
					bean.setStbasedn(objMap.get("st_parametro").toString());
				
					
					 if(objMap.get("va_valor")==null){
						 bean.setVaBasedn("");
						}else{
						 bean.setVaBasedn(objMap.get("va_valor").toString());
					}
					
					if(bean.getStbasedn().equalsIgnoreCase("Y")){
						 bean.setChkBasedn(true);
						}else{
						 bean.setChkBasedn(false);
						}
					
				}else if(i==4){
					 objMap = (HashMap<String, Object>) lista.get(i);
					 bean.setUid(objMap.get("id").toString());
					 bean.setDeuid(objMap.get("de_parametro").toString());
					 bean.setStuid(objMap.get("st_parametro").toString());
					
					 
					 if(objMap.get("va_valor")==null){
						 bean.setVaUid("");
						}else{
						 bean.setVaUid(objMap.get("va_valor").toString());
					}
					 
					 if(bean.getStuid().equalsIgnoreCase("Y")){
						 bean.setChkUid(true);
						}else{
							bean.setChkUid(false);
						}
					 
				}else if(i==5){
					 objMap =(HashMap<String, Object>)  lista.get(i);
					 bean.setTiempomaximoventa(objMap.get("id").toString());
					 bean.setDetiempomaximoventa(objMap.get("de_parametro").toString());
					 bean.setSttiempomaximoventa(objMap.get("st_parametro").toString());
					
					 
					 if(objMap.get("va_valor")==null){
						 bean.setVaTiempomaximoventa("");
						}else{
						 bean.setVaTiempomaximoventa(objMap.get("va_valor").toString());
					}
					 
					 if(bean.getSttiempomaximoventa().equalsIgnoreCase("Y")){
						 bean.setChkTiempomaximoventa(true);
						}else{
							bean.setChkTiempomaximoventa(false);
						}
				}else if(i==6){
					objMap =(HashMap<String, Object>)  lista.get(i);
					bean.setCanjesmaximosventa(objMap.get("id").toString());
					bean.setDecanjesmaximosventa(objMap.get("de_parametro").toString());
					bean.setStcanjesmaximosventa(objMap.get("st_parametro").toString());
					
					
					if(objMap.get("va_valor")==null){
						 bean.setVaCanjesmaximosventa("");
						}else{
						 bean.setVaCanjesmaximosventa(objMap.get("va_valor").toString());
					}
					
					if(bean.getStcanjesmaximosventa().equalsIgnoreCase("Y")){
						 bean.setChkCanjesmaximosventa(true);
						}else{
							bean.setChkCanjesmaximosventa(false);
						}
				}else if(i==7){
					objMap =(HashMap<String, Object>)  lista.get(i);
					bean.setConsumominimoventa(objMap.get("id").toString());
					bean.setDeconsumominimoventa(objMap.get("de_parametro").toString());
					bean.setStconsumominimoventa(objMap.get("st_parametro").toString());
					
					
					if(objMap.get("va_valor")==null){
						 bean.setVaConsumominimoventa("");
						}else{
						 bean.setVaConsumominimoventa(objMap.get("va_valor").toString());
					}
					
					
					if(bean.getStconsumominimoventa().equalsIgnoreCase("Y")){
						 bean.setChkConsumominimoventa(true);
						}else{
							bean.setChkConsumominimoventa(false);
						}
					
				}else if(i==8){
					objMap =(HashMap<String, Object>)  lista.get(i);
					bean.setDiasvencimiento(objMap.get("id").toString());
					bean.setDediasvencimiento(objMap.get("de_parametro").toString());
					bean.setStdiasvencimiento(objMap.get("st_parametro").toString());
					
					
					if(objMap.get("va_valor")==null){
						 bean.setVaDiasvencimiento("");
						}else{
						 bean.setVaDiasvencimiento(objMap.get("va_valor").toString());
					}
					
					
					if(bean.getStdiasvencimiento().equalsIgnoreCase("Y")){
						 bean.setChkDiasvencimiento(true);
						}else{
							bean.setChkDiasvencimiento(false);
						}
					
				}else if(i==9){
					objMap =(HashMap<String, Object>)  lista.get(i);
					bean.setTiempoesperasolcanjeporregularizar(objMap.get("id").toString());
					bean.setDetiempoesperasolcanjeporregularizar(objMap.get("de_parametro").toString());
					bean.setSttiempoesperasolcanjeporregularizar(objMap.get("st_parametro").toString());
					
					
					if(objMap.get("va_valor")==null){
						 bean.setVatiempoesperasolcanjeporregularizar("");
						}else{
						 bean.setVatiempoesperasolcanjeporregularizar(objMap.get("va_valor").toString());
					}
					
					
					if(bean.getSttiempoesperasolcanjeporregularizar().equalsIgnoreCase("Y")){
						 bean.setChktiempoesperasolcanjeporregularizar(true);
						}else{
							bean.setChktiempoesperasolcanjeporregularizar(false);
						}
					
				}
				
				
			}
		}
		   
		
		return bean;
	}

	@Override
	public int actualizarVariablesGlobales(BNVariablesSistema bean) {
		
		
		StringBuffer query= new StringBuffer();
		
		query.append(" update clpb_parametro set va_valor=?,");
		query.append("        st_parametro=? where id=? ");
		
		int i=0;
		
		 i=jdbcTemplate.update(query.toString(), new Object[]{bean.getVavalor(),bean.getStparametro(),bean.getId()});
		
		return i;
			
		}
	
	

}
